import type { bd_material, bd_metal, bd_section, bd_location, t_material_stock } from '@/types/basedata'
import type { pd_board, pd_coil, pd_cutsolution, pd_qrcode, pd_stick } from '@/types/producedata'
import type {
  coils_cut_order,
  cut_demand,
  cut_demand_sum,
  cut_process,
  divide_demand,
  stock_order,
  divide_demand_sum,
  stick_propose_group,
  arrive_bill,
  move_bill,
  require_order,
  t_reserv_bill,
  mtl_call,
  pick_task,
  pick_record
} from '@/types/billdata'
import coils from '@/meta/coils.json'
import metal from '@/meta/metal.json'
import cutdemandbill from '@/meta/cutdemandbill.json'
import cutsolution from '@/meta/cutsolution.json'
import stick from '@/meta/stick.json'
import section from '@/meta/section.json'
import dividedemandbill from '@/meta/dividedemandbill.json'
import board from '@/meta/board.json'
import material from '@/meta/material.json'
import location from '@/meta/location.json'
import qrcode from '@/meta/qrcode.json'
import stockorder from '@/meta/stockorder.json'
import requireorder from '@/meta/requireorder.json'

import { defineStore } from 'pinia'
import { e_material_storage_status } from '@/types/enum'

const qrcode_as_json = sessionStorage.getItem('_qrcode_')
const coils_as_json = sessionStorage.getItem('_coils_')
const metal_as_json = sessionStorage.getItem('_metal_')
const cutdemandbill_as_json = sessionStorage.getItem('_cutdemandbill_')
const cutsolution_as_json = sessionStorage.getItem('_cutsolution_')
const stick_as_json = sessionStorage.getItem('_stick_')
const section_as_json = sessionStorage.getItem('_section_')
const dividedemandbill_as_json = sessionStorage.getItem('_dividedemandbill_')
const board_as_json = sessionStorage.getItem('_board_')
const material_as_json = sessionStorage.getItem('_material_')
const location_as_json = sessionStorage.getItem('_location_')
const stockorder_as_json = sessionStorage.getItem('_stockorder_')
const cut_demand_sum_json = sessionStorage.getItem('_cut_demand_sum_')
const coils_cut_order_json = sessionStorage.getItem('_coils_cut_order_')
const cut_process_json = sessionStorage.getItem('_cut_process_')
const divide_demand_json = sessionStorage.getItem('_divide_demand_')
const divide_demand_sum_json = sessionStorage.getItem('_divide_demand_sum_')
const stick_propose_json = sessionStorage.getItem('_stick_propose_')
const arrive_json = sessionStorage.getItem('_arrive_')
const move_json = sessionStorage.getItem('_move_')
const require_order_json = sessionStorage.getItem('_require_order_')
const material_stock_json = sessionStorage.getItem('_material_stock_')
const reserv_bill_json = sessionStorage.getItem('_reserv_bill_')
//const reserv_qrcode_json = sessionStorage.getItem('_reserv_qrcode_')
const mtl_call_json = sessionStorage.getItem('_mtl_call_')
const pick_json = sessionStorage.getItem('_pick_')
const pick_record_json = sessionStorage.getItem('_pick_record_')

const _qrcode = (qrcode_as_json ? JSON.parse(qrcode_as_json) : qrcode) as pd_qrcode[]
const _coils = (coils_as_json ? JSON.parse(coils_as_json) : coils) as pd_coil[]
const _metal = (metal_as_json ? JSON.parse(metal_as_json) : metal) as bd_metal[]
const _cutdemandbill = (cutdemandbill_as_json ? JSON.parse(cutdemandbill_as_json) : cutdemandbill) as cut_demand[]
const _cutsolution = (cutsolution_as_json ? JSON.parse(cutsolution_as_json) : cutsolution) as pd_cutsolution[]
const _stick = (stick_as_json ? JSON.parse(stick_as_json) : stick) as pd_stick[]
const _section = (section_as_json ? JSON.parse(section_as_json) : section) as bd_section[]
const _dividedemandbill = (dividedemandbill_as_json ? JSON.parse(dividedemandbill_as_json) : dividedemandbill) as divide_demand[]
const _board = (board_as_json ? JSON.parse(board_as_json) : board) as pd_board[]
const _material = (material_as_json ? JSON.parse(material_as_json) : material) as bd_material[]
const _location = (location_as_json ? JSON.parse(location_as_json) : location) as bd_location[]
const _stockorder = (stockorder_as_json ? JSON.parse(stockorder_as_json) : stockorder) as stock_order[]
const _cut_demand_sum = (cut_demand_sum_json ? JSON.parse(cut_demand_sum_json) : []) as cut_demand_sum[]
const _coils_cut_order = (coils_cut_order_json ? JSON.parse(coils_cut_order_json) : []) as coils_cut_order[]
const _cut_process = (cut_process_json ? JSON.parse(cut_process_json) : []) as cut_process[]
const _divide_demand = (divide_demand_json ? JSON.parse(divide_demand_json) : []) as divide_demand[]
const _divide_demand_sum = (divide_demand_sum_json ? JSON.parse(divide_demand_sum_json) : []) as divide_demand_sum[]
const _stick_propose = (stick_propose_json ? JSON.parse(stick_propose_json) : []) as stick_propose_group[]
const _arrive = (arrive_json ? JSON.parse(arrive_json) : []) as arrive_bill[]
const _move = (move_json ? JSON.parse(move_json) : []) as move_bill[]
const staff = ['蒋琬', '邓芝', '杨仪', '费祎']
const _require_order = (require_order_json ? JSON.parse(require_order_json) : requireorder) as require_order[]
const setup_material_stock = () => {
  const result: t_material_stock[] = []
  _qrcode
    .filter(
      (r) =>
        r.status === e_material_storage_status.上架 ||
        r.status === e_material_storage_status.合格 ||
        r.status === e_material_storage_status.特采
    )
    .forEach((r) => {
      const materialcode = typeof r.material === 'string' ? r.material : r.material.code
      const exist = result.find((s) => s.code === materialcode)
      if (exist) {
        exist.qty += r.qty
        exist.avaliable += r.qty
      } else {
        result.push({ code: materialcode, avaliable: r.qty, reserv: 0, qty: r.qty, locked: 0 })
      }
    })
  return result
}
const _material_stock = (material_stock_json ? JSON.parse(material_stock_json) : setup_material_stock()) as t_material_stock[]
const _reserv_bill = (reserv_bill_json ? JSON.parse(reserv_bill_json) : []) as t_reserv_bill[]
//const _reserv_qrcode = (reserv_qrcode_json ? JSON.parse(reserv_qrcode_json) : []) as t_reserv_qrcode[]
const _mtl_call = (mtl_call_json ? JSON.parse(mtl_call_json) : []) as mtl_call[]
const _pick = (pick_json ? JSON.parse(pick_json) : []) as pick_task[]
const _pick_record = (pick_record_json ? JSON.parse(pick_record_json) : []) as pick_record[]

export const useDbStore = defineStore({
  id: 'db',
  state: () => {
    return {
      qrcode: _qrcode,
      coils: _coils,
      metal: _metal,
      cutdemandbill: _cutdemandbill,
      cutsolution: _cutsolution,
      stick: _stick,
      section: _section,
      dividedemandbill: _dividedemandbill,
      board: _board,
      material: _material,
      location: _location,
      stockorder: _stockorder,
      cut_demand_sum: _cut_demand_sum,
      coils_cut_order: _coils_cut_order,
      cut_process: _cut_process,
      divide_demand: _divide_demand,
      divide_demand_sum: _divide_demand_sum,
      stick_propose: _stick_propose,
      arrive: _arrive,
      move: _move,
      staff: staff,
      require_order: _require_order,
      materialStock: _material_stock,
      reserv_bill: _reserv_bill,
      //  reserv_qrcode: _reserv_qrcode,
      mtl_call: _mtl_call,
      pick: _pick,
      pick_record: _pick_record
    }
  },
  actions: {
    savePickRecord(items?: pick_record[]) {
      if (items) this.pick_record = items
      sessionStorage.setItem('_pick_record_', JSON.stringify(this.pick_record))
    },
    savePickTask(items?: pick_task[]) {
      if (items) this.pick = items
      sessionStorage.setItem('_pick_', JSON.stringify(this.pick))
    },
    addPickTask(item: pick_task) {
      this.pick.push(item)
      sessionStorage.setItem('_pick_', JSON.stringify(this.pick))
    },
    saveMtlCall(items?: mtl_call[]) {
      if (items) this.mtl_call = items
      sessionStorage.setItem('_mtl_call_', JSON.stringify(this.mtl_call))
    },
    addMtlCall(item: mtl_call) {
      this.mtl_call.push(item)
      sessionStorage.setItem('_mtl_call_', JSON.stringify(this.mtl_call))
    },
    /* addReservQrCode(item: t_reserv_qrcode) {
      this.reserv_qrcode.push(item)
      sessionStorage.setItem('_reserv_qrcode_', JSON.stringify(this.reserv_qrcode))
    },
    saveReservQrCode(items?: t_reserv_qrcode[]) {
      if (items) this.reserv_qrcode = items
      sessionStorage.setItem('_reserv_qrcode_', JSON.stringify(this.reserv_qrcode))
    },*/
    addReservBill(item: t_reserv_bill) {
      this.reserv_bill.push(item)
      sessionStorage.setItem('_reserv_bill_', JSON.stringify(this.reserv_bill))
    },
    saveReservBill(items?: t_reserv_bill[]) {
      if (items) this.reserv_bill = items
      sessionStorage.setItem('_reserv_bill_', JSON.stringify(this.reserv_bill))
    },

    setMaterialStock(code: string, qty: number) {
      const exists = this.materialStock.find((r) => r.code === code)
      if (exists) {
        exists.qty += qty
        exists.avaliable = exists.qty - exists.reserv
      } else {
        this.materialStock.push({ code: code, qty: qty, reserv: 0, avaliable: qty, locked: 0 })
      }
      sessionStorage.setItem('_material_stock_', JSON.stringify(this.materialStock))
    },
    addQrCode(item: pd_qrcode) {
      this.qrcode.push(item)
      sessionStorage.setItem('_qrcode_', JSON.stringify(this.qrcode))
    },
    saveQrCode(items?: pd_qrcode[]) {
      if (items) this.qrcode = items
      sessionStorage.setItem('_qrcode_', JSON.stringify(this.qrcode))
    },
    saveRequireOrder(items?: require_order[]) {
      if (items) this.require_order = items
      sessionStorage.setItem('_require_order_', JSON.stringify(this.require_order))
    },
    saveLocation(items?: bd_location[]) {
      if (items) this.location = items
      sessionStorage.setItem('_location_', JSON.stringify(this.location))
    },
    addArriveCode(item: arrive_bill) {
      this.arrive.push(item)
      sessionStorage.setItem('_arrive_', JSON.stringify(this.arrive))
    },
    saveArriveCode(items?: arrive_bill[]) {
      if (items) this.arrive = items
      sessionStorage.setItem('_arrive_', JSON.stringify(this.arrive))
    },
    saveCutDemandBill(items?: cut_demand[]) {
      if (items) this.cutdemandbill = items
      sessionStorage.setItem('_cutdemandbill_', JSON.stringify(this.cutdemandbill))
    },
    saveCutDemandSum(items?: cut_demand_sum[]) {
      if (items) this.cut_demand_sum = items
      sessionStorage.setItem('_cut_demand_sum_', JSON.stringify(this.cut_demand_sum))
    },
    saveCoilsCutOrder(items?: coils_cut_order[]) {
      if (items) this.coils_cut_order = items
      sessionStorage.setItem('_coils_cut_order_', JSON.stringify(this.coils_cut_order))
    },
    saveCutProcess(items?: cut_process[]) {
      if (items) this.cut_process = items
      sessionStorage.setItem('_cut_process_', JSON.stringify(this.cut_process))
    },
    saveDivideDemand(items?: divide_demand[]) {
      if (items) this.divide_demand = items
      sessionStorage.setItem('_divide_demand_', JSON.stringify(this.divide_demand))
    },
    saveDivideDemandSum(items?: divide_demand_sum[]) {
      if (items) this.divide_demand_sum = items
      sessionStorage.setItem('_divide_demand_sum_', JSON.stringify(this.divide_demand_sum))
    },
    saveStickProposeGroup(items?: stick_propose_group[]) {
      if (items) this.stick_propose = items
      sessionStorage.setItem('_stick_propose_', JSON.stringify(this.stick_propose))
    },
    saveStockInBill(items?: stock_order[]) {
      if (items) this.stockorder = items
      sessionStorage.setItem('_stockorder_', JSON.stringify(this.stockorder))
    },
    saveMoveBill(items?: move_bill[]) {
      if (items) this.move = items
      sessionStorage.setItem('_move_', JSON.stringify(this.move))
    },
    addMoveBill(item: move_bill) {
      this.move.push(item)
      sessionStorage.setItem('_move_', JSON.stringify(this.move))
    },
    reservMaterialStock(code: string, reserv: number) {
      const exists = this.materialStock.find((r) => r.code === code)
      if (exists) {
        exists.reserv += reserv
        exists.avaliable = exists.qty - exists.reserv - exists.locked
      } else {
        this.materialStock.push({ code: code, qty: 0, reserv: reserv, avaliable: -reserv, locked: 0 })
      }
      sessionStorage.setItem('_material_stock_', JSON.stringify(this.materialStock))
    },
    lockMaterialStock(code: string, locked: number) {
      const exists = this.materialStock.find((r) => r.code === code)
      if (exists) {
        exists.locked += locked
        exists.avaliable = exists.qty - exists.reserv - exists.locked
      } else {
        this.materialStock.push({ code: code, qty: 0, reserv: 0, avaliable: -locked, locked: locked })
      }
      sessionStorage.setItem('_material_stock_', JSON.stringify(this.materialStock))
    },
    reservArriveBill(code: string, reserv: number) {
      const requireBill = this.arrive.find((r) => r.billcode === code)!
      requireBill.reserved_qty += reserv
      this.saveArriveCode()
      return requireBill
    },
    reservStockOrder(code: string, reserv: number) {
      const stockOrder = this.stockorder.find((r) => r.billcode === code)!
      stockOrder.reserved_qty += reserv
      this.saveStockInBill()
      return stockOrder
    },
    reservRequrieOrder(code: string, reserv: number) {
      const require_order = this.require_order.find((r) => r.billcode === code)!
      require_order.reserved_qty += reserv
      this.saveRequireOrder()
      return require_order
    }
  }
})
